package com.etogc.groupchat.group.adpt;

/**
 * Created by McGrady on 2017/6/11.
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etogc.groupchat.R;
import com.etogc.util.YcToastHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.rong.imkit.RongIM;

/**
 * 来访记录适配器
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> {

    private final Context mContext;

    private List<HashMap<String, String>> mList = new ArrayList<>();

    public SearchAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<HashMap<String, String>> list) {
        if (list != null) {
            this.mList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.group_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Map<String, String> map = mList.get(position);
//        Utils.setCircleImage(mContext, user.getPhotoUrl(), holder.photo);
        String groupName = map.get("group_name");
        final String groupID = map.get("id");
        String inviteCode = map.get("invite_code");

        if (!TextUtils.isEmpty(groupName)) {
            holder.tvGroupName.setText(String.valueOf(groupName) + "(" + String.valueOf(groupID) + ")");
        } else {
            holder.tvGroupName.setText("未命名");
        }
        holder.tvInviteId.setText(String.valueOf(inviteCode));
//
//        holder.age.setText(user.getAge() + "岁");
//        //男性显示自己当前位置
//        String sex = SharedPreferencesUtils.getString(mContext, "sex", "");
//        int visitorSex = mList.get(position).getSex();
//
//
//        if (sex.equals("男")) {
//            if (!TextUtils.isEmpty(Constant.mPosition)) {
//                holder.position.setText(Constant.mPosition);
//            } else {
//                if (!TextUtils.isEmpty(user.getCityName())) {
//                    holder.position.setText(user.getCityName());
//                }
//            }
//        } else {
//
//            if (!TextUtils.isEmpty(user.getCityName())) {
//                holder.position.setText(user.getCityName());
//            }
//        }
//
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
//        holder.time.setText(simpleDateFormat.format(user.getVisitorTime()));
//
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    RongIM.getInstance().startGroupChat(mContext, groupID, "哈哈哈");
                } catch (IllegalArgumentException e) {
                    YcToastHelper.getInstance()._toast("群号异常");
                }
            }
        });
//
//        if (2 == visitorSex) {
//            holder.ivSex.setImageResource(R.drawable.female);
//        } else {
//            holder.ivSex.setImageResource(R.drawable.male);
//        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivGroupHead;
        TextView tvGroupName, tvInviteId;
        View item;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivGroupHead = (ImageView) itemView.findViewById(R.id.ivGroupHead);
            tvGroupName = (TextView) itemView.findViewById(R.id.tvGroupName);
            tvInviteId = (TextView) itemView.findViewById(R.id.tvInviteId);
            item = itemView.findViewById(R.id.rootView);
        }
    }
}